package main

import (
	"github.com/gogap/logs"
	"log"
	"yundian/internel/app/dao"
	"yundian/internel/app/models/orders"
	"yundian/internel/app/models/users"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

// 同步关联关系
//INSERT INTO orders_user_relevancy (user_id, trade_no)
//SELECT dl.user_id,dl.trade_no FROM dividends_log dl LEFT JOIN orders o ON o.trade_no=dl.trade_no
//WHERE dl.user_id>0 AND o.state=5 AND dl.trade_no<>'' GROUP BY user_id, trade_no;

func main() {
	//dingo.InitViper("./configs/adm.yaml")
	//dingo.InitViper("./configs/adm.yaml")
	dingo.InitViper("./configs/zhongman.yaml")
	dbs.NewMysqlComponent().Step()

	var orderListUse []struct {
		orders.OrderModel
		BranchUserId int    `gorm:"column:branch_user_id" json:"branchUserId"`
		EndDate      string `gorm:"column:end_date"`
		EndMonth     string `gorm:"column:end_month"`
	}
	//orderSql := `SELECT o.*,p.branch_user_id,DATE_FORMAT(FROM_UNIXTIME(o.end_at), '%Y-%m-%d')end_date,DATE_FORMAT(
	//FROM_UNIXTIME(o.end_at), '%Y-%m')end_month FROM orders o LEFT JOIN places p ON o.place_id=p.id
	//WHERE o.user_id>0 AND o.state<>5 ORDER BY o.id DESC`
	//orderSql := `SELECT o.*,p.branch_user_id,DATE_FORMAT(FROM_UNIXTIME(o.end_at), '%Y-%m-%d')end_date,DATE_FORMAT(
	//FROM_UNIXTIME(o.end_at), '%Y-%m')end_month FROM orders o LEFT JOIN places p ON o.place_id=p.id WHERE trade_no NOT IN
	//(SELECT trade_no FROM dividends_log) AND o.user_id>0 AND place_id>0`
	orderSql := `SELECT o.*,p.branch_user_id,DATE_FORMAT(FROM_UNIXTIME(o.end_at), '%Y-%m-%d')end_date,DATE_FORMAT(FROM_UNIXTIME(o.end_at), '%Y-%m')end_month
FROM orders o LEFT JOIN places p ON o.place_id=p.id WHERE trade_no IN
(
                                           'ZM20210805214955526033',
                                           'ZM20210805222041325134',
                                           'ZM20210805233611475845',
                                           'ZM20210806072845020384',
                                           'ZM20210806153554014222',
                                           'ZM20210807235013759053',
                                           'ZM20210808094236757726',
                                           'ZM20210809005831688752',
                                           'ZM20210809153748186957',
                                           'ZM20210810012017747390',
                                           'ZM20210810210508195242',
                                           'ZM20210811004221400784',
                                           'ZM20210811201729660093',
                                           'ZM20210811204544097712',
                                           'ZM20210812131339916466',
                                           'ZM20210812224936135679',
                                           'ZM20210813204049034450',
                                           'ZM20210814035556384366',
                                           'ZM20210814221359156742',
                                           'ZM20210814235829930275',
                                           'ZM20210815002200306192',
                                           'ZM20210815082339907714',
                                           'ZM20210815210551233552',
                                           'ZM20210816003032843543',
                                           'ZM20210816044754224069',
                                           'ZM20210816220020537014',
                                           'ZM20210816223009198295',
                                           'ZM20210816235544083281',
                                           'ZM20210817014246786925',
                                           'ZM20210817022703727429',
                                           'ZM20210819114504427473',
                                           'ZM20210819230234006050',
                                           'ZM20210820215543071018',
                                           'ZM20210821012516944659',
                                           'ZM20210822115422968724',
                                           'ZM20210822224309713737',
                                           'ZM20210823185450098091',
                                           'ZM20210823230507454842',
                                           'ZM20210824022854763574',
                                           'ZM20210824053632045236',
                                           'ZM20210824200406720398',
                                           'ZM20210825001549755149',
                                           'ZM20210825074442527773',
                                           'ZM20210825084048768015',
                                           'ZM20210825120733102755',
                                           'ZM20210825174302046483',
                                           'ZM20210825225449379384',
                                           'ZM20210826034505780347',
                                           'ZM20210826044223300858',
                                           'ZM20210826202732049234',
                                           'ZM20210827072531895014'
                                              )`
	if db := global.Orm.Debug().Raw(orderSql).Find(&orderListUse); db.Error != nil {
		logs.Error(db.Error)
		return
	}

	userMap := map[int][]int{}
	insertList := []map[string]interface{}{}
	for index, item := range orderListUse {
		if userMap[item.UserId] == nil {
			userMap[item.UserId] = toRepetition(dao.NewUserDao().QueryParentList(item.UserId, item.BranchUserId).Unwrap().([]*users.UserModel))
		}
		for _, userId := range userMap[item.UserId] {
			insertList = append(insertList, map[string]interface{}{
				"user_id":   userId,
				"id":        item.Id,
				"trade_no":  item.TradeNo,
				"end_date":  item.EndDate,
				"end_month": item.EndMonth,
			})
		}

		if len(insertList) >= 10000 || index == len(orderListUse)-1 {
			log.Println(item)
			if db := global.Orm.Table("orders_user_relevancy").Create(insertList); db.Error != nil {
				log.Println("orders_user_relevancy ERR", db.Error, item)
				return
			}
			insertList = insertList[:0]
			log.Println("成功", item)
		}
	}
}

func toRepetition(list []*users.UserModel) []int {
	var newList []int
	for _, item := range list {
		off := true
		for _, id1 := range newList {
			if item.ID == id1 {
				off = false
				break
			}
		}
		if off {
			newList = append(newList, item.ID)
		}
	}
	return newList
}
